package com.dzt.androidframework.express;

import android.annotation.SuppressLint;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.dzt.androidframework.R;
import com.dzt.androidframework.constant.AppConfig;
import com.dzt.androidframework.database.dao.TaskDao;
import com.dzt.androidframework.eventbus.EventBusUtil;
import com.dzt.androidframework.express.javabean.OrderDetailEvent;
import com.dzt.androidframework.express.javabean.OrderEvent;
import com.dzt.androidframework.express.javabean.OrderInfoBean;
import com.dzt.androidframework.express.javabean.OrderInfoLitePal;
import com.dzt.androidframework.express.javabean.OrderState;
import com.dzt.androidframework.express.net.MyStringCallBack;
import com.dzt.androidframework.express.net.Ok_Request;
import com.dzt.androidframework.fragment.FragmentBase;
import com.dzt.androidframework.recycler.SlideRecyclerAdapter;
import com.dzt.androidframework.utils.JLoadDlgKit;
import com.dzt.androidframework.utils.JToastKit;
import com.dzt.androidframework.widgets.ScrollChildSwipeRefreshLayout;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by M02323 on 2017/9/19.
 */
@ContentView(R.layout.fragment_order)
public class FragmentOrder extends FragmentBase {

	private int type = OrderState.TYPE_ALL;
	@ViewInject(R.id.refresh_layout)
	private ScrollChildSwipeRefreshLayout swipeRefreshLayout;
	@ViewInject(R.id.rv_fragment_order_signed)
	private RecyclerView recyclerView;

	private List<OrderInfoLitePal> datas = new ArrayList<>();
	private OrderAdapter adapter;

	@SuppressLint("ValidFragment")
	private FragmentOrder() {
		super();
	}

	@SuppressLint("ValidFragment")
	private FragmentOrder(int type) {
		this();
		this.type = type;
	}

	public static FragmentOrder newInstance(int type) {
		return new FragmentOrder(type);
	}

	@Override
	protected void initWidgets() {
		recyclerView.setLayoutManager(new LinearLayoutManager(context));
		adapter = new OrderAdapter(context, datas, R.layout.item_order, type);
		swipeRefreshLayout.setColorSchemeColors(
				ContextCompat.getColor(getActivity(), R.color.colorPrimary),
				ContextCompat.getColor(getActivity(), R.color.colorAccent),
				ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark)
		);
		// Set the scrolling view in the custom SwipeRefreshLayout.
		swipeRefreshLayout.setScrollUpChild(recyclerView);
		swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh() {
				//下拉刷新
				swipeRefreshLayout.post(new Runnable() {
					@Override
					public void run() {
						QueryData();
						adapter.notifyDataSetChanged();
						swipeRefreshLayout.setRefreshing(false);
					}
				});
			}
		});
		adapter.setOnItemClickListener(new onItemClickListener());
		recyclerView.setAdapter(adapter);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onEvent(OrderEvent response) {
		if (response.from.equals("ExpressActivity")) {
			if (datas.size() > 0) {
				if (response.needclose) {
					adapter.slideClose();
					for (OrderInfoLitePal order : datas) {
						order.setIsSelect(false);
					}
					adapter.notifyDataSetChanged();
				} else {
					adapter.slideOpen();
				}
			}
		}
	}

	public class onItemClickListener implements SlideRecyclerAdapter.OnItemClickListener {
		@Override
		public void onItemClick(View view, int position) {
			if (adapter.isEditing())
				return;
			OrderInfoLitePal info = datas.get(position);
			HashMap<String, String> map = new HashMap<>();
			map.put("type", info.getCom());//参数
			map.put("postid", info.getNu());
			Ok_Request.getAsyncData(getActivity(), map, AppConfig.GetKuaiDi, new MyStringCallBack() {
				@Override
				public void onError(Call call, Exception e) {
					JToastKit.getInstance().showMessage(e.getMessage());
				}

				@Override
				public void onResponse(OrderInfoBean response) {
					if (response != null) {
						EventBusUtil.postSticky(new OrderDetailEvent(response, context));
						startActivity(OrderDetailActivity.class, null);
					}
				}

				@Override
				public void onBefore(Request request) {
					JLoadDlgKit.showProgressDialog(context, true);
				}

				@Override
				public void onAfter() {
					JLoadDlgKit.hideProgressDialog();
				}
			});
		}
	}

	/**
	 * 避免调用notify失败
	 */
	public void QueryData() {
		datas.clear();
		TaskDao dao = TaskDao.getInstance();
		if (type == OrderState.TYPE_ALL) {
			List<OrderInfoLitePal> lists = dao.findAllExpress();
			if (lists != null) {
				datas.addAll(lists);
			}
		} else if (type == OrderState.TYPE_SIGNED) {
			List<OrderInfoLitePal> lists = dao.findSignedExpress();
			if (lists != null) {
				datas.addAll(lists);
			}
		} else if (type == OrderState.TYPE_UNSIGNED) {
			List<OrderInfoLitePal> lists = dao.findUNSignedExpress();
			if (lists != null) {
				datas.addAll(lists);
			}
		}
	}
}
